package com.djzhou.gmms.position.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.company.TbCompany;
import com.djzhou.gmms.model.department.TbDepartment;
import com.djzhou.gmms.model.position.TbPosition;
import com.opensymphony.xwork2.ModelDriven;

public class PositionAction extends BaseAction<TbPosition> implements ModelDriven<TbPosition> {

	private TbPosition position;
	private Integer positionId;
	private Integer departmentId;
	private List<TbPosition> list;
	private List<TbDepartment> departmentList;
	
	public String configDefault(){
		positionService.updatePositionDefault(positionId);
		msg="设置成功";
		setCurrentPage(currentPage);
		logService.addLog("职位管理",queryUser(),"设置默认",positionId,"TbPosition",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}
	
	@Override
	public String delete() {
		positionService.delete(positionId);
		msg="删除成功";
		setCurrentPage(currentPage);
		logService.addLog("职位管理",queryUser(),"删除",positionId,"TbPosition",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String modify() {
		TbDepartment department=departmentService.queryById(departmentId);
		position.setDepartment(department);
		position.setCompany(queryCompany());
		position.setLastModifyTime(new Date());
		position.setUser(queryUser());
		position.setDeleteFlag(0);
		positionService.update(position);
		msg="修改成功";
		setCurrentPage(currentPage);
		logService.addLog("职位管理",queryUser(),"编辑",positionId,"TbPosition",queryCompany(),super.queryDepartment());
		return SUCCESS;
	}

	@Override
	public String queryList() {
		if(loadData==null){
			departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
			return SUCCESS;
		}
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		
		if(departmentId!=null && !departmentId.equals("1") && !departmentId.equals(""))hashMap.put("departmentId", departmentId);
		
		pageBean=positionService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean=positionService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbPosition>();
		}
		totalPage=pageBean.getTotalPage();
		return SUCCESS;
	}

	@Override
	public String save() {
		if(position!=null){
			TbDepartment department=departmentService.queryById(departmentId);
			position.setDepartment(department);
			Date date=new Date();
			position.setCompany(queryCompany());
			position.setCreateTime(date);
			position.setLastModifyTime(date);
			position.setDeleteFlag(0);
			position.setUser(queryUser());
			positionService.add(position);
			logService.addLog("职位管理",queryUser(),"添加",positionId,"TbPosition",queryCompany(),super.queryDepartment());
		}
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		position=positionService.queryById(positionId);
		return super.toEdit();
	}

	public TbPosition getModel() {
		return position;
	}

	public TbPosition getPosition() {
		return position;
	}

	public void setPosition(TbPosition position) {
		this.position = position;
	}

	public Integer getPositionId() {
		return positionId;
	}

	public void setPositionId(Integer positionId) {
		this.positionId = positionId;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public List<TbPosition> getList() {
		return list;
	}

	public void setList(List<TbPosition> list) {
		this.list = list;
	}

	public List<TbDepartment> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<TbDepartment> departmentList) {
		this.departmentList = departmentList;
	}
	

}
